import tkinter
import subprocess

top = tkinter.Tk()
# kill -9 $(ps -ef | grep python | grep -v vscode | grep -v grep | awk "{print $ 2}")
# kill -9 $(ps -ef | grep xrdp | grep -v grep | awk '{print $ 2}')
# alias kill54='kill -9 $(ps -ef | grep python | grep -v vscode | grep -v grep | awk "{print $ 2}")'
# colcon build && . /project45/install/setup.bash && ros2 launch mas.py
class Manager(object):
   def __init__(self):
      self.index = 1

   def initEnv(self):
      subprocess.call(['ros2 run py_virtual_env service --ros-args --remap __name:=coordinator&'], shell=True)
      
   def addUAV(self):
      subprocess.call(['ros2 run py_agent uav  --ros-args --remap __name:=uav_%d&'%self.index], shell=True)
      self.index+=1
      
   def addUAVplanner(self):
      subprocess.call([r'kill -9 $(ps -ef | grep python | grep -v vscode | grep -v grep | awk "{print $ 2}")'], shell=True)

   def addWoker(self):
      subprocess.call(['ros2 run host_worker service  --ros-args --remap __name:=hostworker&'], shell=True)

   def rqt(self):
      subprocess.call(['rqt&'], shell=True)

   def searchzone(self):
      cmd = {
         "summary_unity":
            {
               'unity命令_创建搜索区域': [200,200,800,800]
            }
         
      }
      import json
      cmd_json = json.dumps(cmd)
      cmd_byte = cmd_json.encode('utf-8')
      import redis
      self.redis = redis.Redis(host='127.0.0.1', port=6379)
      self.redis.lpush('summary_unity', cmd_byte)

   def global_search_enable(self):
      cmd = {
         "summary_unity":
            {
               'unity命令_全局调试': ''
            }
         
      }
      import json
      cmd_json = json.dumps(cmd)
      cmd_byte = cmd_json.encode('utf-8')
      import redis
      self.redis = redis.Redis(host='127.0.0.1', port=6379)
      self.redis.lpush('summary_unity', cmd_byte)


   def debug(self):
      import numpy as np
      def rand1D():
         return np.random.rand()*2.0 - 1.0
      def rand2D(x):
         res = np.array(  [rand1D(),rand1D()]  )*x + np.array([500,500])
         return res.tolist()
      
      cmd = {
         "summary_unity":
            {
               'unity命令_创建地面目标': 
               {
                  '参数_敌地面单位数量': 1, '参数_敌地面单位位置': 
                     [                  
                        rand2D(250)
                     ], 
                  '参数_友地面单位数量': 1, '参数_友地面单位位置': 
                     [
                        rand2D(250)
                     ]
               }
            }
         
      }

      import json
      cmd_json = json.dumps(cmd)
      cmd_byte = cmd_json.encode('utf-8')
      import redis
      self.redis = redis.Redis(host='127.0.0.1', port=6379)
      self.redis.lpush('summary_unity', cmd_byte)




      # cmd = {
      #       "summary_unity":
      #          {'unity命令_新建无人机': {'参数_新建无人机数量': 5, '参数_新无人机初始位置': [[323.385529, 119.425728], [414.145264, 106.168953], [478.6648, 95.7375946], [532.8591, 130.083679], [536.3895, 95.7231]], '参数_新无人机负载': '', '参数_新无人机覆盖半径': 150.0, '参数_新无人机通信半径': 375.0, '参数_新无人机最大速度': 500, '参数_新无人机最大加速度': 10}}         
      #    }
      
      # import json
      # cmd_json = json.dumps(cmd)
      # cmd_byte = cmd_json.encode('utf-8')
      # import redis
      # self.redis = redis.Redis(host='127.0.0.1', port=6379)
      # self.redis.lpush('summary_unity', cmd_byte)
      return 
'''
第二次发{
  "summary_unity": {

    "unity命令_新建无人机":{
        "参数_新建无人机数量": 3,
        "参数_新无人机初始位置": [
            [6.956469,5.451076],
            [20.64747739,40.16074657],
            [2.64747739,4.16074657]
        ],
        "参数_新无人机负载": [
            "","",""
        ]
    }


  }
}

第三次发{
  "summary_unity": {


    "unity命令_创建搜索区域":[0,0, 222,222], //对角线的两点[x1，y1; x2, y2]


  }
}
'''
manager = Manager()
B_initEnv = tkinter.Button(top, text ="<1> init simulation", command = manager.initEnv)
B_initEnv.pack()

B_addhostwoker = tkinter.Button(top, text ="<2> add woker", command = manager.addWoker)
B_addhostwoker.pack()

debug = tkinter.Button(top, text ="<3> add dual ground targets", command = manager.debug)
debug.pack()

B_addUAV = tkinter.Button(top, text ="<4-1> add a uav", command = manager.addUAV)
B_addUAV.pack()

B_searchzone = tkinter.Button(top, text ="<4-2> add a searchzone", command = manager.searchzone)
B_searchzone.pack()

global_searchzone = tkinter.Button(top, text ="<4-3> toggle global search", command = manager.global_search_enable)
global_searchzone.pack()

B_rqt_planner = tkinter.Button(top, text ="<5> kill everything", command = manager.addUAVplanner)
B_rqt_planner.pack()

B_rqt = tkinter.Button(top, text ="open rqt", command = manager.rqt)
B_rqt.pack()





top.mainloop()